import { commonManageSearchParams, getLoadMapList, getSelectDataMapList, getTenantID, getUserID, requestFunComponFiex, requestFunHtml } from '@/utils';
import { useRequest } from 'umi';
import { ILoadMapTypeAll } from '@/common';

import { useCallback, useMemo, useState } from 'react';
import { AssetacquisitionManageAPI } from '../common/type';
import { ManageSearchColumns } from '../mconfig';
import { disposeArea } from '../common';

import { AssetacquisitionTabSwitch } from '../mconfig/ManageTabSwitch';
import { getBaseUrl } from '@/services/urlconst';

/** 盘点汇报管理页 service */
export const InventorySummaryManageServices = (curRes: Record<string, any>) => {
  /**获取权限参数 */
  const queryTypeObj: Record<string, any> = AssetacquisitionTabSwitch(curRes);

  /***获取数据字典**/
  const { loadMapInfo, mapLoading } = getLoadMapList([
    `${getTenantID()}${ILoadMapTypeAll.ASSET_SUMMARY_STATUS}`, //资产类型
  ]);
  const assetType = getSelectDataMapList(loadMapInfo?.[`${getTenantID()}${ILoadMapTypeAll.ASSET_SUMMARY_STATUS}`], 'name', 'id', true);

  /** 获取搜索参数 */
  const searchListParams = ManageSearchColumns(assetType);

  /***保存当前的搜索参数**/
  const [manageTableParams, setManageTableParams] = useState<Record<string, any>>({});

  /***管理页获取数据方法**/
  const { tableProps, data, loading, run } = useRequest(
    (reqParams: AssetacquisitionManageAPI.IAssetTableRequestType) => {
      let PostParams = {
        ...queryTypeObj,
        current: 1,
        pageSize: 10,
        sort: 'createTime',
        order: 'desc',
        queryType: 0,
        currentUserID: getUserID(),
        ...reqParams,
      };

      /***保存一份参数**/
      setManageTableParams(PostParams);

      const reqUrl = `${getBaseUrl()}/assetInventoryDifference/dataGrid/${queryTypeObj?.params}`;

      /***发起请求数据接口**/
      return requestFunComponFiex('POST', reqUrl, PostParams);
    },
    {
      manual: true,
      paginated: true,
      defaultPageSize: 10,
    },
  );

  /***重新请求的方法**/
  const reloadRequest = useCallback((reqParams: AssetacquisitionManageAPI.IAssetTableRequestType | any) => {
    /**处理创建时间*/
    if (reqParams?.createTime) {
      const [timeStart, timeEnd] = reqParams.createTime;
      reqParams['createTimeStart'] = timeStart.format('YYYY-MM-DD 00:00:00');
      reqParams['createTimeEnd'] = timeEnd.format('YYYY-MM-DD 23:59:59');
      delete reqParams['createTime'];
    }

    /**处理签发时间*/
    if (reqParams?.signTime) {
      const [timeStart, timeEnd] = reqParams.signTime;
      reqParams['signTimeStart'] = timeStart.format('YYYY-MM-DD 00:00:00');
      reqParams['signTimeEnd'] = timeEnd.format('YYYY-MM-DD 23:59:59');
      delete reqParams['signTime'];
    }

    /**处理修改时间*/
    if (reqParams?.modifyTime) {
      const [timeStart, timeEnd] = reqParams.modifyTime;
      reqParams['modifyTimeStart'] = timeStart.format('YYYY-MM-DD 00:00:00');
      reqParams['modifyTimeEnd'] = timeEnd.format('YYYY-MM-DD 23:59:59');
      delete reqParams['modifyTime'];
    }

    /**处理作废时间*/
    if (reqParams?.cancelTime) {
      const [timeStart, timeEnd] = reqParams.cancelTime;
      reqParams['cancelTimeStart'] = timeStart.format('YYYY-MM-DD 00:00:00');
      reqParams['cancelTimeEnd'] = timeEnd.format('YYYY-MM-DD 23:59:59');
      delete reqParams['cancelTime'];
    }

    delete reqParams['createFormNameDept'];
    delete reqParams['createFormNameUser'];
    if (reqParams?.createrID && reqParams?.createrID?.length > 0) {
      reqParams['createrID'] = reqParams?.createrID?.toString();
    } else {
      delete reqParams['createrID'];
    }

    delete reqParams['cancelFormNameDept'];
    delete reqParams['cancelFormNameUser'];
    if (reqParams?.cancelerID && reqParams?.cancelerID?.length > 0) {
      reqParams['cancelerID'] = reqParams?.cancelerID?.toString();
    } else {
      delete reqParams['cancelerID'];
    }

    run({ ...reqParams });
  }, []);

  /***页面数据及分页信息**/
  const tableValues = useMemo(() => {
    return {
      data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  /***搜索功能配置columns**/
  const searchList = useMemo(() => {
    return {
      searchList: searchListParams,
    };
  }, [searchListParams]);

  return {
    loading,
    reloadRequest,
    manageTableParams,
    ...tableValues, //页面列表值
    ...searchList,
  };
};

// 结束盘点
export const completeServices = (curRes: Record<string, any>, callback) => {
  const authority: any = AssetacquisitionTabSwitch(curRes);
  const request = useRequest(
    (reqParams: AssetacquisitionManageAPI.IAssetTableRequestType) => {
      let PostParams = {
        params: authority.params,
        createrID: authority.createrID,
        createDepartmentID: authority.createDepartmentID,
        inventoryState: `${getTenantID()}${ILoadMapTypeAll.ASSET_INNENTORY_STATUS}1005`,
        ...reqParams,
      };
      const reqbody: Record<string, any> = disposeArea(PostParams);
      let reqUrl;
      // 物料管理
      reqUrl = `${getBaseUrl()}/assetInventory/terminate/${reqParams.id}`;
      /***发起请求数据接口**/
      return requestFunComponFiex('POST', reqUrl, reqbody);
    },
    {
      manual: true,
      paginated: true,
      defaultPageSize: 10,
      onSuccess: callback,
    },
  );

  return {
    completeRun: request.run,
    taskPushLoading: request.loading,
    taskPushPagination: request.pagination,
  };
};

// 导出盘点
export const ExportServices = (callback: (res: any) => void, curRes: Record<string, any>) => {
  const authority: any = AssetacquisitionTabSwitch(curRes);
  const request = useRequest(
    (reqParams: AssetacquisitionManageAPI.IAssetTableRequestType) => {
      let PostParams = {
        params: authority.params,
        createrID: authority.createrID,
        createDepartmentID: authority.createDepartmentID,
        ...reqParams,
      };
      const reqbody: Record<string, any> = disposeArea(PostParams);
      let reqUrl;
      // 物料管理
      reqUrl = `${getBaseUrl()}/assetInventory/export/${reqParams.id}`;
      /***发起请求数据接口**/
      return requestFunHtml('POST', reqUrl, reqbody);
    },
    {
      manual: true,
      paginated: true,
      onSuccess: callback,
    },
  );

  return {
    ExportRun: request.run,
    taskPushLoading: request.loading,
    taskPushPagination: request.pagination,
  };
};
